# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=log4cxx
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.0
pkgrel=2
pkgdesc="log4cxx C++ logging framework. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://logging.apache.org/log4cxx'
msys2_repository_url="https://github.com/apache/logging-log4cxx"
msys2_references=(
  'archlinux: log4cxx'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-apr"
         "${MINGW_PACKAGE_PREFIX}-apr-util"
         "${MINGW_PACKAGE_PREFIX}-fmt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-base")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-base: Qt5 Support")
source=("http://archive.apache.org/dist/logging/${_realname}/${pkgver}/apache-${_realname}-${pkgver}.tar.gz"
        "001-install-pkgconfig-file-on-mingw.patch"
        "002-fix-build-with-mingw-w64-gcc.patch")
sha256sums=('09f4748aa5675ef5c0770bedbf5e00488668933c5a935a43ac5b85be2436c48a'
            '61cd0402cfe3ce907645586cc0fe3d98fcc124181067aa5f273a5fd6e67a480f'
            'fc468fc2231de49858d11116429ccc6689c6bafb3a643d6156997eeaf1072a1c')

prepare() {
  cd "${srcdir}"/apache-${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-install-pkgconfig-file-on-mingw.patch
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    patch -Np1 -i "${srcdir}"/002-fix-build-with-mingw-w64-gcc.patch
  fi
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -DWIN32" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DLOG4CXX_QT_SUPPORT=ON \
      -DBUILD_TESTING=OFF \
      -S "apache-${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/apache-${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
